// page/component/new-pages/user/address/address.js
Page({
  data:{
    name: '',
    phone: '',
    detail: '',
    region: ['陕西省', '西安市', '长安区'],
    isAbove: true,
    array: [],
    index: 0,
    num: null,
    userID: null
  },

  onLoad (query) {
    let that = this;
    console.log(query.num);
    this.setData({
      num: query.num
    })
    console.log('address');
    wx.request({
      url: 'https://localhost:80/commuity/query',
      method: 'get',
      header: {
        'content-type': 'application/json' 
      },
      success (res) {
        console.log(res.data);
        console.log("----------===");
        let areaArray = [];
        for (let i = 0; i < res.data.length; i++)
        {
          areaArray[i] = res.data[i].cName
        }
        that.setData({
          array: areaArray,
        })
      }
    });
    console.log('address');
  },

  formSubmit (e) {
    const value = e.detail.value;
    let that = this;
    wx.getStorage({
      key: 'userID',
      success(res) {
        that.setData({
          userID: res.data
        }) 
      }
    });
    let userId = this.data.userID;
    if (userId == null) {
      userId = getApp().globalData.userID
    }
    console.log(userId);
    if (value.name && value.phone && value.detail && value.region) {
      let str = /^1\d{10}$/;
      let judge = str.test(value.phone);
      if (!judge) {
        wx.showToast({
          title: '手机格式错误',
          icon: 'none',
          duration: 1000
        })
      } else {
        wx.request({
          url: 'https://localhost:80/address/save',
          method: 'post',
          data: {      
            addrArea: value.region[2],
            addrCity: value.region[1],
            addrCommuity: this.data.array[value.area],
            addrDetail: value.detail,
            addrId: null,
            addrNickname: value.name,
            addrPhone: value.phone,
            addrProvince: value.region[0],
            addrState: 1,
            addrUser: userId,
            addrZipcode: ""
          },
          header: {
            'content-type': 'application/x-www.form-urlencoded' 
          },
          success (res) {  
            console.log(res);
            console.log('------');
            wx.showToast({
              title: '地址保存成功',
              icon: 'success',
              duration: 1000
            })
            if (that.data.num == '1') {
              wx.navigateTo({
                url: '../takeaddress/takeaddress'
              })
            } else {
              wx.navigateTo({
                url: '../alladdress/alladdress'
              })
            }
          }
        })    
      }
    } else {
      wx.showModal({
        title:'提示',
        content:'请填写完整资料',
        showCancel:false
      })
    }
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  }, 

  bindPickerChange: function (e) {
    let num = e.detail.value;
    console.log('picker发送选择改变，携带值为', this.data.array[num]);
    this.setData({
      index: e.detail.value
    })
  },
})